#ifndef SGU_TEXT_H
#define SGU_TEXT_H

SGU_NS_BEGIN

class TextOutput : NoCopy, public UsedObj
{
	public:
	TextOutput() {}
	virtual ~TextOutput() {}

	virtual void write(char) = 0;
	virtual void write(const char *) = 0;
	virtual void writeln() = 0;
	virtual void writeln(const char *) = 0;

	void write(const std::string & str) {write(str.data());}
	void writeln(const std::string & str) {writeln(str.data());}

	static bool valid(char k) {return inco((int)k,32,128);} 
};

class TextInput : NoCopy, public UsedObj
{
	public:
	TextInput(char q = 127) : _q(q) {}
	virtual ~TextInput() {} 

	virtual bool read(std::string &) = 0;
	virtual std::string read() = 0; // throws exception for EOF

	char validate(char k) {return valid(k) ? k : _q;}

	static bool valid(char k) {return inco((int)k,32,128);} 

	private:
	char _q;
};



class TextOutputFile : public TextOutput
{
	public:
	TextOutputFile(const char * filename, mode_t mode, size_t growby, t_destructretry drt = 0) :
		_file(filename,true,mode,growby,drt), _ptr(0) {}

	virtual ~TextOutputFile() {}

	virtual void write(char);
	virtual void write(const char *);
	virtual void writeln();
	virtual void writeln(const char *);

	private:
	FileMappingDynamicSegment _file;
	size_t _ptr;

	void writeChar(char);
	
};

class TextInputFile : public TextInput
{
	public:
	TextInputFile(const char * filename, char q, t_destructretry drt = 0) :
		TextInput(q), _file(filename,drt), _ptr(0) {}

	virtual ~TextInputFile() {} 

	virtual bool read(std::string &);
	virtual std::string read();

	private:
	FileMappingConstSegment _file;
	size_t _ptr;

	int readChar();
};


SGU_NS_END

#endif

